package com.apusic.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.apusic.common.Constants;
import com.apusic.common.PageInfo;
import com.apusic.entity.*;
import com.apusic.service.BaseResourceService;
import com.apusic.service.BaseVersionService;
import com.apusic.service.DistrictService;
import com.apusic.util.Base64Util;
import com.apusic.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;


@Controller
@RequestMapping(value = "/system")
public class SystemController {
	
	@Autowired
	BaseVersionService baseVersionService;
	
	@Autowired
	BaseResourceService baseResourceService;
	
	@Autowired
	private DistrictService districtService;
	/**跳转到用户新增页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/userApply")
	public String showUserApply(HttpServletRequest request,
			HttpServletResponse response) {
		return "/system/sysUser/user_apply";
	}
	
	/**查询用户列表
	 * @param request
	 * @param response
	 * @return
	 */
	/*@RequestMapping(value = "/queryUser")
	@ResponseBody
	public String showIndex(HttpServletRequest request,
			HttpServletResponse response) {
		List list=userService.selectAll();
		String result=JsonUtils.objectToJson(list);
		return result;
	}*/
	
	/**跳转到版本管理页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/versionApply")
	public String showVersionApply(HttpServletRequest request,
			HttpServletResponse response) {
		return "/system/baseVersion/version_apply";
		//return "user/userDistrict";
	}
	/**跳转到版本新增页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/versionAdd")
	public String showVersionAdd(HttpServletRequest request,
			HttpServletResponse response) {
		TBaseDistrict district=new TBaseDistrict();
		district.setParentCode("100000");
		district.setLevel("1");
		List<TBaseDistrict> list = districtService.getDistrictList(district);
		request.setAttribute("provinceList", list);
		return "/system/baseVersion/version_add";
	}
	
	/**跳转到版本修改页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/versionEdit")
	public String showVersionEdit(HttpServletRequest request,
			HttpServletResponse response,String versionId) {
		request.setAttribute("versionId", versionId);
		return "/system/baseVersion/version_edit";
	}
	
	/**检查版本名称
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/checkVersionName")
	@ResponseBody
	public String checkVersionName(HttpServletRequest request,
			HttpServletResponse response,String versionName) {
		BaseVersion bVersion=new BaseVersion();
		bVersion.setVersionName(versionName);
		List<BaseVersion> list=baseVersionService.selectByObjList(bVersion);
		return JsonUtils.objectToJson(list);
		//return "user/userDistrict";
	}
	
	/**查询版本信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/queryAllVersion")
	@ResponseBody
	public String queryAllVersion(HttpServletRequest request,
			HttpServletResponse response,@RequestParam(name="pageNum",defaultValue="0" ) int pageNum,
			@RequestParam(name="pageSize",defaultValue="20") int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		BaseVersion bVersion=new BaseVersion();
		List<BaseVersion> list=baseVersionService.selectByObjList(bVersion);
		List<BaseVersion> resultList=new ArrayList<BaseVersion>();
		if(list.size()!=0){
			for (int i = 0; i < list.size(); i++) {
				BaseVersion baseVersion=list.get(i);
				String versionPass=baseVersion.getVersionPass();
				String modelString="";
				if(versionPass!=null&&!"".equals(versionPass)){
					String modelStr= Base64Util.decode(versionPass);
					String[] models=modelStr.split("_");
					Map<Object, Object> paramMap = new HashMap<>();
					List<Object> modelList=new ArrayList<Object>();
					if(models.length!=0){
						for (int j = 0; j < models.length; j++) {
							modelList.add(models[j]);
						}
					}
					paramMap.put("modelList", modelList);
					List<BaseResource> listResource=baseResourceService.queryByUrl(paramMap);
					
					if(listResource.size()!=0){
						for (int n = 0; n < models.length; n++) {
							modelString+=listResource.get(n).getResourceName();
							modelString+=",";
						}
					}
					if(!"".equals(modelString)){
						modelString=modelString.substring(0, modelString.length()-1);
					}
				}
				baseVersion.setVersionModel(modelString);
				resultList.add(baseVersion);
			}
			
		}
		PageInfo<TUser> pageInfo = new PageInfo(resultList);
		String str = JsonUtils.objectToJson(pageInfo);
		return str;
	}
	
	/**查询版本信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/queryVersion")
	@ResponseBody
	public String queryVersion(HttpServletRequest request,
			HttpServletResponse response,BaseVersion bVersion) {
		
		List<BaseVersion> list=baseVersionService.selectByObjList(bVersion);
		List<BaseVersion> resultList=new ArrayList<BaseVersion>();
		if(list.size()!=0){
			for (int i = 0; i < list.size(); i++) {
				BaseVersion baseVersion=list.get(i);
				String versionPass=baseVersion.getVersionPass();
				String modelString="";
				if(versionPass!=null&&!"".equals(versionPass)){
					modelString=Base64Util.decode(versionPass);
				}
				baseVersion.setVersionPass(modelString);
				resultList.add(baseVersion);
			}
		}
		String str = JsonUtils.objectToJson(resultList);
		return str;
	}
	/**
	 * 保存版本信息
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/saveVersion")
	public String saveVersion(HttpServletRequest request,
			HttpServletResponse response,BaseVersion bVersion){
		
		String province=bVersion.getProvince();
		String city=bVersion.getCity();
		String county=bVersion.getCounty();
		
		if(county!=null&&!county.equals("")){
			bVersion.setDistrictCode(county);
		}else if(city!=null&&!city.equals("")){
			bVersion.setDistrictCode(city);
		}else if(province!=null&&!province.equals("")){
			bVersion.setDistrictCode(province);
		}
		
		String versionPass=bVersion.getVersionPass();
		String resultPassStr="";
		if(versionPass!=null&&!"".equals(versionPass)){
			String[] versionPasss=versionPass.split("_");
			for (int i = 0; i < versionPasss.length; i++) {
				if(versionPasss[i]!=null&&!"0".equals(versionPasss[i])&&!"undefined".equals(versionPasss[i])){
					resultPassStr+=versionPasss[i];
					resultPassStr+="_";
				}
			}
			resultPassStr=resultPassStr.substring(0, resultPassStr.length()-1);
		}
		String resultPass=Base64Util.encode(resultPassStr);
		bVersion.setVersionPass(resultPass);
		bVersion.setOperateDate(new Date());
		TSystemUser user = (TSystemUser)request.getSession().getAttribute(Constants.Session_key);
		bVersion.setOperatePeople(user.getUserName());
		baseVersionService.saveVersion(bVersion);
		return "redirect:/system/versionApply";
	}
	
	/**
	 * 修改版本信息
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/updateVersion")
	public String updateVersion(HttpServletRequest request,
			HttpServletResponse response,BaseVersion bVersion){
		
		String versionPass=bVersion.getVersionPass();
		String resultPassStr="";
		if(versionPass!=null&&!"".equals(versionPass)){
			String[] versionPasss=versionPass.split("_");
			for (int i = 0; i < versionPasss.length; i++) {
				if(versionPasss[i]!=null&&!"0".equals(versionPasss[i])&&!"undefined".equals(versionPasss[i])){
					resultPassStr+=versionPasss[i];
					resultPassStr+="_";
				}
			}
			resultPassStr=resultPassStr.substring(0, resultPassStr.length()-1);
		}
		String resultPass=Base64Util.encode(resultPassStr);
		bVersion.setVersionPass(resultPass);
		bVersion.setOperateDate(new Date());
		TSystemUser user = (TSystemUser)request.getSession().getAttribute(Constants.Session_key);
		bVersion.setOperatePeople(user.getUserName());
		baseVersionService.updateVersion(bVersion);
		return "redirect:/system/versionApply";
	}
}
